
using Android.OS;
using Android.Support.V4.App;
using Android.Views;
using Android.Views.InputMethods;
using Android.Widget;

namespace LePcCtrlApp
{
    public class KeyBoardFragment : Fragment, ISoftKeyEventListener
    {

        private SoftKeyBoardView SoftKeyBoardView;
        private LoginManager m_LoginManager;
        private KeyEventPacket m_keyEventPacket;

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            m_keyEventPacket = new KeyEventPacket();
            m_LoginManager = LoginManager.GetInstance();
            // Create your fragment here
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment
            View relVew=inflater.Inflate(Resource.Layout.KeyboardLayout, container, false);
            LinearLayout viewRoot=(LinearLayout)relVew.FindViewById(Resource.Id.rootLinearLayout);
            SoftKeyBoardView = new SoftKeyBoardView(relVew.Context,null);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
            layoutParams.AddRule(LayoutRules.AlignParentBottom);
            viewRoot.AddView(SoftKeyBoardView,layoutParams);
            SoftKeyBoardView.SetKeyEventReceiver(this);
            
            return relVew;
        }

        public void OnKeyDown(Keycode code)
        {
            m_keyEventPacket.MakeKeyEvent(1,(int)code);
            m_LoginManager.SendPacketAsync(m_keyEventPacket.GetKeyEventPacket());
        }

        public void OnKeyUp(Keycode code)
        {
            m_keyEventPacket.MakeKeyEvent(0, (int)code);
            m_LoginManager.SendPacketAsync(m_keyEventPacket.GetKeyEventPacket());
        }
    }
}